<?php

namespace App\Admin\Actions\Grid\User;

use App\Models\DisableDevice as DisableDeviceModel;
use App\Models\User;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Http\Request;

class OpenDevice extends RowAction
{
    /**
     * @return string
     */
    protected $title = '解禁此设备';

    /**
     * Handle the action request.
     *
     * @return Response
     */
    public function handle(): Response
    {
        $user = User::query()->where('id', '=', $this->getKey())->first();
        if ($user->device && $user->device_number) {
            DisableDeviceModel::open($user->device, $user->device_number);
        }

        return $this
            ->response()
            ->success(trans('admin.succeeded'))
            ->refresh();
    }

    /**
     * @return string
     */
    public function confirm(): string
    {
        return '确定解禁此设备吗?';
    }

    public function html(): string
    {
        $this->defaultHtmlAttribute('href', 'javascript:void(0)');
        return <<<HTML
<a {$this->formatHtmlAttributes()}><i class="feather icon-unlock"> {$this->title()} </i></a>
HTML;

    }
}
